package com.zfast.yugioh.junit;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.zfast.yugioh.antlr4.effect.EffectLexer;
import com.zfast.yugioh.antlr4.effect.EffectParser;
import com.zfast.yugioh.antlr4.effect.impl.EffectBaseListenerImpl;
import com.zfast.yugioh.antlr4.effect.impl.EffectFactory;
import com.zfast.yugioh.bo.EffectBo;
import lombok.SneakyThrows;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author zgn
 * @Description: simJunit
 * @date 2022/1/13 0013
 */
public class effectsJunit {
    //相对路径 eg.1(实体机): D:\IDEAWork\play\private\yu-gi-oh
    String relativelyPath = System.getProperty("user.dir");
    @Test
    public void egMax() {//e.g.
        //region 1.模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 2.数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 3.数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }
    //update Card set race=? from GameDetailBoMe.cards to GameDetailBoMe.handCards
    // where type=? and race=? and DestroyedReason
    // limit 0,(update Card set id=? from GameDetailBoMe.cards to GameDetailBoMe.handCards).Size
    // ②：从自己的手卡·场上把最多2张魔法·陷阱卡送去墓地并且设置属性为XX
    // 才能发动。自己从卡组抽出送去墓地的数量。

    @Test
    public void EffectBo_egMin() {//e.g.
        //2022-06-15 17:35:14 当下只有条件能使用()
        /*String effctStr="update Card set race=? from GameDetailBoMe.cards to GameDetailBoMe.handCards " +
                "where ((type=?) or race=?) and DestroyedReason " +
                "limit 0,1";*/
        String effctStr="update Card from GameDetailBoMe.cards to GameDetailBoMe.handCards limit 0,(update Card set id=? from GameDetailBoMe.cards to GameDetailBoMe.handCards).Size";
        /*EffectBo effectBo=EffectBo.builder()
                .orgExpression(effctStr)
                .build();*/
        //EffectBo effectBo=new EffectBo(effctStr);
        //System.out.println(effectBo.getOrgExpression());
    }

    @Test
    public void EffectBo_数组过滤_egMin() {//e.g.
        Integer[] firstNumAllArry= {0,1,21,-1,5,4};
        Integer[] clone = ArrayUtil.clone(firstNumAllArry);
        clone[0]=-1;
        clone[1]=-1;
        clone[2]=-1;
        Integer[] filter = ArrayUtil.filter(clone, c -> c != -1);
        if(filter.length>0){
            System.out.println(ArrayUtil.min(filter));
        }
    }

    @Test
    public void 使用antlr4的语句拆解egMin() throws IOException {//e.g.
        //实体机
        //CharStream inputStream = CharStreams.fromFileName("E:\\private\\yu-gi-oh\\src\\test\\java\\com\\zfast\\yugioh\\junit\\antlr4\\rec\\g4.txt", Charset.forName("UTF-8"));
        //虚拟机
        //CharStream inputStream = CharStreams.fromFileName("D:\\IDEAWork\\play\\private\\yu-gi-oh\\src\\test\\java\\com\\zfast\\yugioh\\junit\\antlr4\\rec\\g4.txt", Charset.forName("UTF-8"));
        //相对路径
        CharStream inputStream = CharStreams.fromFileName(relativelyPath+"\\src\\test\\java\\com\\zfast\\yugioh\\junit\\antlr4\\rec\\g4.txt", Charset.forName("UTF-8"));
        EffectLexer lexer = new EffectLexer(inputStream);

        //EffectLexer lexer = new EffectLexer(CharStreams.fromString("SELECT IsTrue SET asd FROM gameDetailBoMe TO asdas WHERE whererere LIMIT limisada"));
        CommonTokenStream token = new CommonTokenStream(lexer);
        EffectParser parser = new EffectParser(token);

        ParseTree tree =parser.orgExpression();
        System.out.println(tree.toStringTree());
        ParseTreeWalker walker= new ParseTreeWalker();
        EffectBaseListenerImpl effectBaseListener = new EffectBaseListenerImpl(parser);
        walker.walk(effectBaseListener,tree);
        EffectBo effectBo = effectBaseListener.getEffectBo();

        System.out.println(effectBo.getOrgExpression());
    }


    @Test
    public void RuleContext_egMin() {//e.g.
        EffectParser.Set_exprContext set_exprContext =null;
        int depth = set_exprContext.depth();//深度
        int childCount = set_exprContext.getChildCount();//孩子节点数
        String s = set_exprContext.toStringTree();//lisp 风格打印

        System.out.println();
    }

    @SneakyThrows
    @Test
    public void 测试token工厂_egMin() {//e.g.
        CharStream inputStream = CharStreams.fromFileName(relativelyPath+"\\src\\test\\java\\com\\zfast\\yugioh\\junit\\antlr4\\rec\\g4.txt", Charset.forName("UTF-8"));
        EffectLexer lexer = new EffectLexer(inputStream);

        EffectFactory factory=new EffectFactory(inputStream);
        lexer.setTokenFactory(factory);

        CommonTokenStream token = new CommonTokenStream(lexer);
        token.fill();
        List<Token> tokens = token.getTokens();
        for (int i = 0; i < tokens.size(); i++) {
            Token token1 =  tokens.get(i);
            System.out.println(token1.toString());
        }

        //EffectParser parser = new EffectParser(token);
        System.out.println();
    }
    @Test
    public void egMin() {//e.g.
        System.out.println();
    }
}
